<template>
    <div>
        <el-dialog
        :title="isEdit?'修改':title"
        :visible.sync="dialogVisible"
        width="50%">
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="商品名称">
                <el-input v-model="form.platformGoodsName" clearable></el-input>
            </el-form-item>
            <el-form-item label="商品图片">
                <el-upload
                    class="upload-demo"
                    accept=".jpeg,.png,.jpg,.bmp,.gif,.webp"
                    :action="uploadUrl"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    :headers="headers"
                    multiple
                    :on-exceed="handleExceed"
                    :file-list="form.tbPlatformGoodsFileList"
                    :on-success="handleSuccess"
                    :on-error="handleError"
                    list-type="picture-card"
                    >
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="积分">
                <el-input v-model="form.platformPrice" clearable @change="priceHandle"></el-input>
            </el-form-item>
            <el-form-item label="商品券后价格">
                <el-input v-model="form.finalPrice" clearable></el-input>
            </el-form-item>
            <el-form-item label="库存数量">
                <el-input v-model="form.quantitySold" clearable></el-input>
            </el-form-item>
            <!-- <el-form-item label="活动区域">
                <el-select v-model="form.region" placeholder="请选择活动区域">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="活动时间">
                <el-col :span="11">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
                </el-col>
            </el-form-item> -->
            <el-form-item label="是否启用">
                <el-switch v-model="form.enabled" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <!-- <el-form-item label="活动性质">
                <el-checkbox-group v-model="form.type">
                <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
                <el-checkbox label="地推活动" name="type"></el-checkbox>
                <el-checkbox label="线下主题活动" name="type"></el-checkbox>
                <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="特殊资源">
                <el-radio-group v-model="form.resource">
                <el-radio label="线上品牌商赞助"></el-radio>
                <el-radio label="线下场地免费"></el-radio>
                </el-radio-group>
            </el-form-item> -->
            <el-form-item label="商品描述">
                <el-input type="textarea" v-model="form.productDescription" :show-word-limit="true" rows="4"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitHandle">确 定</el-button>
        </span>
        </el-dialog>
    </div>
</template>

<script>
  import product from '@/api/product/product'
  import { getToken } from '@/utils/auth'
    export default {
        name: 'ProductDialog',
        props: {
            title: {
                type: String,
                default:'新增'
            },
            
        },
        data () {
            return {
                dialogVisible:false,
                isEdit:false,//是否编辑
                fileList: [
                ],
                form: {
                    platformGoodsName:'',//商品名称
                    platformPrice: 0, // 单价
                    finalPrice:0, // 券后价格
                    enabled:0, // 是否启用
                    productDescription:'', // 商品描述
                    tbPlatformGoodsFileList:[],
                    quantitySold:0, // 库存数量	
                    unit:'',//单位
                }
            }
        },
        computed: {
            uploadUrl(){
                return process.env.VUE_APP_BASE_API+'/public/file/upload'
            },
            headers() {
                return { Authorization: getToken() };
            },
        },
        mounted () {
            // alert("sfdsf");
            // for (let index = 0; index < this.fileList.length; index++) {
            //     const element = this.fileList[index];
            //     this.imageList.push(element.url);
            // }      
            // console.log(JSON.stringify(this.imageList));
        },
        methods: {
            priceHandle(val){
                this.form.finalPrice = val;
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.id == file.id) {
                        fileList.splice(index, 1);
                        break;
                    }
                }
                this.form.tbPlatformGoodsFileList = fileList;
            },
            handlePreview(file) {
                console.log(file);
                // let urls = [];
                // for (let index = 0; index < this.fileList.length; index++) {
                //     const element = this.fileList[index];
                //     console.log(element.url);
                //     urls.push(element.url);
                // }
                this.$viewerApi({ images: [file.url] })

                // const viewer = this.$el.querySelector('.images').$viewer
                // viewer.show()
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            handleSuccess(response, file, fileList) {
                // const uid = file.uid
                // const id = response.id
                // this.files.push({ uid, id })
                // console.log(JSON.stringify(response));
                // this.$notify({ message: '上传成功', type: 'success'});
                // this.form.fileList.push(response.data);
                // console.log(JSON.stringify(this.form.fileList));
                // file.fid = file.response.data.fid
                // file.size = file.response.data.size
                // file.format = file.response.data.format

                // this.$notify({ message: '上传成功', type: 'success'});
                let fileList2 = [];
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.raw == null || element.raw == undefined){
                        fileList2.push(element);
                    }else{
                        element.name = element.fileName;
                        element.response.data.name = element.response.data.fileName;
                        fileList2.push(element.response.data);
                    }
                    
                }
                console.log(JSON.stringify(fileList2));
                this.form.tbPlatformGoodsFileList = fileList2;
            },
            handleError(){
                this.$notify({ message: '上传失败', type: 'error'});
            },
            // 提交
            submitHandle(){
                if (this.form.platformGoodsName.trim().length ==0) {
                    this.$message.warning(`商品名称不能为空`);
                    return;
                }
                if (this.form.tbPlatformGoodsFileList.length == 0) {
                    this.$message.warning(`请上传至少一张商品图片`);
                    return;
                }
                if (this.form.platformPrice ==0) {
                    this.$message.warning(`请设置商品价格`);
                    return;
                }
                if (this.isEdit) {//修改
                    product.goodsPlatformUpdate(this.form).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }else{
                    product.goodsPlatformCreate(this.form).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }
            }
        }
    }
</script>

<style lang="scss">

</style>